#include "Inf_TW51.h"

uint8_t RDataBUFF[10];
uint16_t buff_len = 0;
uint16_t TemData_len = 0;
extern uint8_t TemDataBuff[9]; // 接收数据缓存区
extern float temNew;           // /处理后的温度湿度数据
extern float humNew;
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    debug_printfln("RDataBUFF:%s", RDataBUFF);
    if ((huart->Instance) == USART3)
    {
        buff_len = Size;
        HAL_UARTEx_ReceiveToIdle_IT(&huart3, RDataBUFF, sizeof(RDataBUFF));
    }
    else if ((huart->Instance) == USART2)
    {
        TemData_len = Size;
        HAL_UARTEx_ReceiveToIdle_IT(&huart2, TemDataBuff, TemData_len);
    }
}

void Inf_TW51_OLED(void)
{
    debug_printfln("TW51_OLED_Start");
    HAL_UARTEx_ReceiveToIdle_IT(&huart3, RDataBUFF, sizeof(RDataBUFF));
    debug_printf("test111");
    while (1)
    {

        if (buff_len > 0)
        {
            HAL_UART_Transmit(&huart1, "OK!\r\n", 5, 1000);
            debug_printfln("RDataBUFF:%s", RDataBUFF);
            buff_len = 0;
        }

        RS485_ReadData();
        HAL_Delay(20);
        if (strstr((const char *)RDataBUFF, "hello") != NULL)
        {
            memset(RDataBUFF, 0, sizeof(RDataBUFF));
            OLED_ShowString(1, 1, "TW51 hello      ");
            OLED_ShowString(2, 1, "                ");
        }
        else if (strstr((const char *)RDataBUFF, "getTem") != NULL)
        {
            //  temNew; humNew;
            memset(RDataBUFF, 0, sizeof(RDataBUFF));
            debug_printfln("temNew:%f", temNew);
            char tem[17] = {0}; // 'tem:0.0'
            sprintf(tem, "tem:%.2f    ", temNew);

            char hum[17] = {0}; // 'tem:0.0'
            sprintf(hum, "tem:%.2f    ", humNew);
            OLED_ShowString(1, 1, tem);
            OLED_ShowString(2, 1, hum);
        }
        else if (strstr((const char *)RDataBUFF, "unloadTem") != NULL)
        {
            memset(RDataBUFF, 0, sizeof(RDataBUFF));
            OLED_ShowString(1, 1, "getTem success! ");
            OLED_ShowString(2, 1, "                ");
        }
    }
}
